package com.aperos.android15;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.content.Context;
import android.widget.Toast;

public class Service {
	
	public static List<Bar> listBars = new ArrayList<Bar>();
	public static Bar currentBar;
	
	public static void ajoutBar(Bar bar) {
		listBars.add(bar);
	}
	
	public static void initListBars(Context context) throws ClientProtocolException, IOException {
		listBars = new ArrayList<Bar>();
		
		DefaultHttpClient client = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet("http://aperos-pas-chers.appspot.com/service/getAllBars");
		//HttpGet httpGet = new HttpGet("http://www.google.fr");
		
		HttpResponse response = client.execute(httpGet);
		
		BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
		String line;
		while ((line=reader.readLine())!=null){
			StringTokenizer token = new StringTokenizer(line, "|");
			String tab[];
			
			String tok0 = token.nextToken();
			Integer id = Integer.valueOf(tok0.split("=")[1]);
			
			String tok1 = token.nextToken();
			String name = tok1.split("=")[1];
			
			String address = null;
			String tok2 = token.nextToken();
			tab = tok2.split("=");
			if (tab.length > 1) {
				address = tab[1];
			}
			
			String tok3 = token.nextToken();
			Double priceBeer = Double.valueOf(tok3.split("=")[1]).doubleValue();
			
			Double priceCocktail = null;
			String tok4 = token.nextToken();
			tab = tok4.split("=");
			if (tab.length > 1) {
				priceCocktail = Double.valueOf(tab[1]).doubleValue();
			}
			
			Integer happyHourBeginHour = null;
			String tok5 = token.nextToken();
			tab = tok5.split("=");
			if (tab.length > 1) {
        		happyHourBeginHour = Integer.valueOf(tab[1]).intValue();
        	}
			
        	Integer happyHourBeginMinute = null;
        	String tok6 = token.nextToken();
        	tab = tok6.split("=");
			if (tab.length > 1) {
        		happyHourBeginMinute = Integer.valueOf(tab[1]).intValue();
        	}
        	
        	Integer happyHourEndHour = null;
        	String tok7 = token.nextToken();
        	tab = tok7.split("=");
			if (tab.length > 1) {
        		happyHourEndHour = Integer.valueOf(tab[1]).intValue();
        	}
        	
        	Integer happyHourEndMinute = null;
        	String tok8 = token.nextToken();
        	tab = tok8.split("=");
			if (tab.length > 1) {
        		happyHourEndMinute = Integer.valueOf(tab[1]).intValue();
        	}
        	
        	Double priceBeerHappyHour = null;
        	String tok9 = token.nextToken();
        	tab = tok9.split("=");
			if (tab.length > 1) {
        		priceBeerHappyHour = Double.valueOf(tab[1]).doubleValue();
        	}
        	
        	Double priceCocktailHappyHour = null;
        	String tok10 = token.nextToken();
        	tab = tok10.split("=");
			if (tab.length > 1) {
        		priceCocktailHappyHour = Double.valueOf(tab[1]).doubleValue();
        	}
        	
			String tok11 = token.nextToken();
			int latitudeE6 = Integer.valueOf(tok11.split("=")[1]).intValue();
        	
        	String tok12 = token.nextToken();
			int longitudeE6 = Integer.valueOf(tok12.split("=")[1]).intValue();
				
			Bar bar = new Bar(id, latitudeE6, longitudeE6, name, address, priceBeer, priceCocktail, happyHourBeginHour, happyHourBeginMinute, happyHourEndHour, happyHourEndMinute, priceBeerHappyHour, priceCocktailHappyHour);
			ajoutBar(bar);
		}
	   
	}
	
	public static void addBar(Bar bar, Context context) throws ClientProtocolException, IOException {
		DefaultHttpClient client = new DefaultHttpClient();
		
		ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("name", bar.getName()));
        
        if (bar.getAddress() != null) {
        	params.add(new BasicNameValuePair("address", bar.getAddress()));
        } else {
        	params.add(new BasicNameValuePair("address", ""));
        }
        if (bar.getPriceBeer() != null) {
        	params.add(new BasicNameValuePair("priceBeer", bar.getPriceBeer().toString()));
        } else {
        	params.add(new BasicNameValuePair("priceBeer", ""));
        }
        if (bar.getPriceCocktail() != null) {
        	params.add(new BasicNameValuePair("priceCocktail", String.valueOf(bar.getPriceCocktail()).toString()));
        } else {
        	params.add(new BasicNameValuePair("priceCocktail", ""));
        }
        if (bar.getHappyHourBeginHour() != null) {
        	params.add(new BasicNameValuePair("happyHourBeginHour", String.valueOf(bar.getHappyHourBeginHour()).toString()));
        } else {
        	params.add(new BasicNameValuePair("happyHourBeginHour", ""));
        }
        if (bar.getHappyHourBeginMinute() != null) {
        	params.add(new BasicNameValuePair("happyHourBeginMinute", String.valueOf(bar.getHappyHourBeginMinute()).toString()));
        } else {
        	params.add(new BasicNameValuePair("happyHourBeginMinute", ""));
        }
        if (bar.getHappyHourEndHour() != null) {
        	params.add(new BasicNameValuePair("happyHourEndHour", String.valueOf(bar.getHappyHourEndHour()).toString()));
        } else {
        	params.add(new BasicNameValuePair("happyHourEndHour", ""));
        }
        if (bar.getHappyHourEndMinute() != null) {
        	params.add(new BasicNameValuePair("happyHourEndMinute", String.valueOf(bar.getHappyHourEndMinute()).toString()));
        } else {
        	params.add(new BasicNameValuePair("happyHourEndMinute", ""));
        }
        if (bar.getPriceBeerHappyHour() != null) {
        	params.add(new BasicNameValuePair("priceBeerHappyHour", String.valueOf(bar.getPriceBeerHappyHour()).toString()));
        } else {
        	params.add(new BasicNameValuePair("priceBeerHappyHour", ""));
        }
        if (bar.getPriceCocktailHappyHour() != null) {
        	params.add(new BasicNameValuePair("priceCocktailHappyHour", String.valueOf(bar.getPriceCocktailHappyHour()).toString()));
        } else {
        	params.add(new BasicNameValuePair("priceCocktailHappyHour", ""));
        }
        params.add(new BasicNameValuePair("latitudeE6", String.valueOf(bar.getLatitudeE6()).toString()));
        params.add(new BasicNameValuePair("longitudeE6", String.valueOf(bar.getLongitudeE6()).toString()));
        
        String url = "http://aperos-pas-chers.appspot.com/service/addBar?" + URLEncodedUtils.format(params, "UTF-8");
        HttpGet httpGet = new HttpGet(url);
		
		HttpResponse response = client.execute(httpGet);
		
		BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

		
	}
	

	
	public static void editBar(Bar bar, Context context) throws ClientProtocolException, IOException {
		DefaultHttpClient client = new DefaultHttpClient();
		
		ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("id", bar.getId().toString()));
        params.add(new BasicNameValuePair("name", bar.getName()));
        params.add(new BasicNameValuePair("address", bar.getAddress()));
        params.add(new BasicNameValuePair("priceBeer", bar.getPriceBeer().toString()));
        
        if (bar.getAddress() != null) {
        	params.add(new BasicNameValuePair("address", bar.getAddress()));
        } else {
        	params.add(new BasicNameValuePair("address", ""));
        }
        if (bar.getPriceBeer() != null) {
        	params.add(new BasicNameValuePair("priceBeer", bar.getPriceBeer().toString()));
        } else {
        	params.add(new BasicNameValuePair("priceBeer", ""));
        }
        if (bar.getPriceCocktail() != null) {
        	params.add(new BasicNameValuePair("priceCocktail", String.valueOf(bar.getPriceCocktail()).toString()));
        } else {
        	params.add(new BasicNameValuePair("priceCocktail", ""));
        }
        if (bar.getHappyHourBeginHour() != null) {
        	params.add(new BasicNameValuePair("happyHourBeginHour", String.valueOf(bar.getHappyHourBeginHour()).toString()));
        } else {
        	params.add(new BasicNameValuePair("happyHourBeginHour", ""));
        }
        if (bar.getHappyHourBeginMinute() != null) {
        	params.add(new BasicNameValuePair("happyHourBeginMinute", String.valueOf(bar.getHappyHourBeginMinute()).toString()));
        } else {
        	params.add(new BasicNameValuePair("happyHourBeginMinute", ""));
        }
        if (bar.getHappyHourEndHour() != null) {
        	params.add(new BasicNameValuePair("happyHourEndHour", String.valueOf(bar.getHappyHourEndHour()).toString()));
        } else {
        	params.add(new BasicNameValuePair("happyHourEndHour", ""));
        }
        if (bar.getHappyHourEndMinute() != null) {
        	params.add(new BasicNameValuePair("happyHourEndMinute", String.valueOf(bar.getHappyHourEndMinute()).toString()));
        } else {
        	params.add(new BasicNameValuePair("happyHourEndMinute", ""));
        }
        if (bar.getPriceBeerHappyHour() != null) {
        	params.add(new BasicNameValuePair("priceBeerHappyHour", String.valueOf(bar.getPriceBeerHappyHour()).toString()));
        } else {
        	params.add(new BasicNameValuePair("priceBeerHappyHour", ""));
        }
        if (bar.getPriceCocktailHappyHour() != null) {
        	params.add(new BasicNameValuePair("priceCocktailHappyHour", String.valueOf(bar.getPriceCocktailHappyHour()).toString()));
        } else {
        	params.add(new BasicNameValuePair("priceCocktailHappyHour", ""));
        }
        
        
        String url = "http://aperos-pas-chers.appspot.com/service/editBar?" + URLEncodedUtils.format(params, "UTF-8");
        HttpGet httpGet = new HttpGet(url);
        
		HttpResponse response = client.execute(httpGet);
		
		BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

	       
	}
	

	public static void addBarError(int barId, int refError, String message, Context context) throws ClientProtocolException, IOException {
		DefaultHttpClient client = new DefaultHttpClient();
		
		ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("barId", String.valueOf(barId).toString()));
        params.add(new BasicNameValuePair("refBarError", String.valueOf(refError).toString()));
        params.add(new BasicNameValuePair("barErrorMessage", message));
        
        String url = "http://aperos-pas-chers.appspot.com/service/addBarError?" + URLEncodedUtils.format(params, "UTF-8");
        HttpGet httpGet = new HttpGet(url);
		
		HttpResponse response = client.execute(httpGet);
		
		BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        
	 
	}
}
